package cn.csc.module.product.api.category;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.module.product.api.category.dto.CategoryRespDTO;
import cn.csc.module.product.convert.category.CategoryConvert;
import cn.csc.module.product.dal.dataobject.category.CategoryDO;
import cn.csc.module.product.enums.Constant;
import cn.csc.module.product.service.category.CategoryService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.module.product.enums.ApiConstants.VERSION;

/**
 * @author liuchuang
 * @version 1.0
 * @date 2024/1/3 17:07
 * @modify
 */
@RestController // 提供 RESTful API 接口，给 Feign 调用
@DubboService(version = VERSION) // 提供 Dubbo RPC 接口，给 Dubbo Consumer 调用
@Validated
public class CategoryApiImpl implements CategoryApi {

    @Resource
    private CategoryService categoryService;

    @Override
    public CommonResult<List<CategoryRespDTO>> listByOneLevel() {
        return success(categoryService.listByShopIdAndParenId(BigInteger.valueOf(Constant.PLATFORM_SHOP_ID), BigInteger.valueOf(Constant.CATEGORY_ID)));
    }

    @Override
    public CommonResult<List<BigInteger>> listCategoryId(BigInteger categoryId) {
        CategoryDO category = categoryService.getCategory(categoryId);
        List<BigInteger> categoryIds = categoryService.listCategoryId(category.getShopId(), category.getParentId());

        return success(categoryIds);
    }

    @Override
    public CommonResult<List<CategoryRespDTO>> listCategoryIds(Collection<BigInteger> ids) {
        List<CategoryDO> list = categoryService.getCategoryList(ids);
        return success(CategoryConvert.INSTANCE.convertList03(list));
    }
}
